package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.department;
import com.qiyeji.real_estate.service.DepartmentService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/Department")
@RestController
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @PostMapping("/All_Department")
    public Result getAllDepartment() {
        List<department> Department = departmentService.getAllDepartment();

        if (Department == null) {
            return Result.error();
        }
        else
            return Result.success(Department);
    }

    @PostMapping("/Add_Department")
    public Result addDepartment(@RequestBody Map<String, String> params) {
        Integer res = departmentService.addDepartment(new department(
                params.get("dCode"),
                params.get("dName")
        ));
        if (res == null) {
            return Result.error();
        }
        else
            return Result.success(res);
    }

    @PostMapping("/Delete_Department")
    public Result deleteDepartment(@RequestBody Map<String, String> params) {
        Integer res = departmentService.deleteDepartment(params.get("dCode"));

        if (res == null){
            return Result.error();
        }
        else
            return Result.success(res);
    }

    @PostMapping("/Alter_Department")
    public Result alterDepartment(@RequestBody Map<String, String> params) {
        Integer res = departmentService.alterDepartment(new department(
                params.get("dCode"),
                params.get("dName")
        ));

        if (res == null)
            return Result.error();
        else
            return Result.success(res);
    }

    @PostMapping("/Query_Department")
    public Result queryDepartment(@RequestBody Map<String, String> params){
        department res = departmentService.queryDepartment(params.get("dCode"));

        if (res == null) {
            return Result.error();
        }
        else
            return Result.success(res);
    }


}
